import { VUE_APP_API_URL } from "@/config/config.js";
import {qs} from './qs.js'

class RequestUtil {

    // 超时时间（毫秒）
    requestTimeout = 10000
    // 默认参数
    defaultOptions = {}


    /**
     * 请求
     * @param options
     * @return {Promise<any>}
     */
    doRequest(options = {}) {
        options = this.beforeRequest(options)
		
        return new Promise((resolve, reject) => {
			const requestUrl = `${ VUE_APP_API_URL }${ options.url }`
            uni.request({
                ...options,
                url: requestUrl,
                timeout: this.requestTimeout,
                success: (res) => {
					if(res.statusCode !== 200){
						console.error(err,"网络请求错误")
						return
					}
                    this.afterResponse(res, resolve, reject)
                },
                fail: (err) => {
                    uni.showToast({title: '请求出错了', icon: 'none'})
                    console.error(err,"网络请求错误")
                    reject(err)
                }
            })
        })
    }

    /**
     * 请求拦截器
     * @param options
     */
    beforeRequest(options) {
        // 合并options，用户优先
        options = {
            header: {},
            ...this.defaultOptions,
            ...options
        }
        // 设置header
        options.header = {
            ...options.header,
        }
        // 处理get参数被吃掉问题
        if (options.method && options.method.toLowerCase() === 'get' && options.data) {
            // 如果是get请求，且params是数组类型如arr=[1,2]，则转换成arr=1&arr=2
            const data = qs.stringify(options.data);
            if (data) {
                delete options.data
                options.url = `${ options.url }?${ data }`;
            }
        }
        return options
    }

    /**
     * 响应拦截器（处理器）
     * @param res {RequestSuccessCallbackResult} 响应
     * @param resolve
     * @param reject
     */
    afterResponse(res, resolve, reject) {
        uni.hideLoading()
        if (res.errMsg === 'uploadFile:ok') {
            res.data = JSON.parse(res.data)
        }
        // 请求statusCode非200
        if (res.statusCode !== 200) {
            return reject(res.data)
        }
        const {data} = res
        // 拦截后端响应code
		const code = data.code || data.status
        if (![0, 200].includes(data.code)) {
            setTimeout(() => {
                uni.showToast({title: data.msg, icon: 'none'})
            }, 50)
            return reject(data)
        }
        resolve(data.data)
    }

    /**
     * get
     * @param url
     * @param data
     * @return {Promise<*>}
     */
    get(url, data = {}) {
        return this.doRequest({
            url,
            data,
            method: 'GET'
        })
    }

    /**
     * post
     * @param url
     * @param data
     * @return {Promise<*>}
     */
    post(url, data = {}) {
        return this.doRequest({
            url,
            data,
            method: 'POST'
        })
    }

    /**
     * put
     * @param url
     * @param data
     * @return {Promise<*>}
     */
    put(url, data = {}) {
        return this.doRequest({
            url,
            data,
            method: 'PUT'
        })
    }

    /**
     * delete
     * @param url
     * @param data
     * @return {Promise<*>}
     */
    delete(url, data = {}) {
        return this.doRequest({
            url,
            data,
            method: 'DELETE'
        })
    }

    /**
     * 上传
     * @param options
     * @return {Promise<unknown>}
     * @doc https://zh.uniapp.dcloud.io/api/request/network-file.html
     */
    upload(options) {
        options = this.beforeRequest(options)
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                ...options,
                url: `${ baseUrl() }${ options.url || VUE_APP_UPLOAD_URL }`,
                success: (uploadFileRes) => {
                    this.afterResponse(uploadFileRes, resolve, reject)
                },
                fail(err) {
                    reject(err)
                }
            })
        })
    }

    /**
     * 下载
     * @param url
     * @return {Promise<unknown>}
     * @doc https://zh.uniapp.dcloud.io/api/request/network-file.html
     */
    download(url) {
        return new Promise((resolve, reject) => {
            Request.downLoadTask = uni.downloadFile({
                url,
                success: (res) => {
                    if (res.statusCode === 200) {
                        return resolve(res)
                    }
                    return reject(res)
                },
                fail: (err) => {
                    reject(err)
                }
            });
        })
    }
}


export const requestUtil = new RequestUtil()
